package com.example.demo.Controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.GeneralResponse.APIResponse;
import com.example.demo.Service.EmployeeService;
import com.example.demo.Service.HRService;
import com.example.demo.Service.TokenService;
import com.example.demo.utils.NotNullDetect;


/**
 * @version: V0.3
 * @author: 郑皓杰 吴嘉鑫
 * @className: LoginController
 * @packageName: com.example.demo.Controller
 * @description: 这是登陆控制器类
 * @data: 2021-07-14
 **/

@RequestMapping("/Login")
@RestController
public class LoginController {

	@Autowired
	private HRService hrService;
	@Autowired 
	private EmployeeService empService;
	@Autowired
	private TokenService tokenService;
	
	
	/**
	* @author:  郑皓杰 吴嘉鑫
	* @methodsName: loginhr
	* @description: hr登录
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/LoginHR")
	public APIResponse<?> loginhr(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		String state = httpServletResponse.getHeader("state");
		if (state.equals("INVALIDTOKEN")) return APIResponse.fail("unauthorizedlogin");
		String username = httpServletRequest.getParameter("username");
		String password = httpServletRequest.getParameter("password");
		if(!NotNullDetect.detect(username,password))  return APIResponse.fail("nmsl");//throw new ProcessException("用户名或密码为空");
		String result=hrService.login(username, password);
		if(result.equals("successlogin"))	{
			String token = tokenService.getToken(username, password);
			httpServletResponse.setHeader("Token",token);
			return APIResponse.success(result,null);
			}
		else return APIResponse.fail(result);
	}
	
	
	
	/**
	* @author:  郑皓杰 吴嘉鑫
	* @methodsName: loginemp
	* @description: employee登录
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/LoginEmp")
	public APIResponse<?> loginemp(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		String username = httpServletRequest.getParameter("username");
		String password = httpServletRequest.getParameter("password");
	//	String result=(String) empService.login(username, password).getData();
	//	if(result.equals("successlogin"))	{
	//		String token = tokenService.getToken(username, password);
	//		httpServletResponse.setHeader("Token",token);
		//	return APIResponse.success("successlogin");}
		//else 
			return empService.login(username, password);//APIResponse.fail("failurelogin");
	}
	
	
}
